<?php


declare(strict_types=1);

namespace app\operations\controller\admin;

use app\admin\model\auth\Admin as AdminModel;
use app\operations\model\Category as CategoryModel;
use app\operations\model\Brand as BrandModel;
use app\shop\model\Category as shopCategoryModel;
use app\operations\model\Store as StoreModel;
use sheep\controller\Backend;
use think\facade\Db;
use think\Request;
use think\Response;

//品牌申请
class CategoryAudit extends Backend
{

    protected $model = null;
    protected $storeModel = null;


    protected function initialize()
    {
        $this->model = new CategoryModel;
        $this->storeModel = new StoreModel;
    }


    /**
     * 查看
     *
     * @return Response
     */
    public function list(Request $request): Response
    {

        $list = $this->model->with('category')->sheepFilter()->paginate(request()->param('list_rows', 10))->toArray();

        foreach($list['data'] as &$val){
            $val['store'] = $this->storeModel->where('organ_id',$val['organ_id'])->where('type',$val['type'])->find();
            if(!empty($val['category'])){
                $val['category_text'] = shopCategoryModel::where('id',$val['category']['parent_id'])->value('name').'>'.$val['category']['name'];
            }else{
                $val['category_text'] = '';
            }

            $val['business_license']  = AdminModel::where('organ_id',$val['organ_id'])->value('business_license');
        }

        return success('获取成功', $list);

    }


    //审核
    public function audit($id){

        $id = intval($id);
        $params = $this->request->only(['audit_status', 'remarks']);

        if(in_array($params['audit_status'],['apply','normal','disabled','reject'])) {
            Db::transaction(function () use ($id,$params) {
                $apply = $this->model->where('id',$id)->find();
                $apply->audit_status = $params['audit_status'] ?? 'apply';
                $apply->remarks = $params['remarks'] ?? '';
                $apply->save();
            });
            return success('审核成功');
        }
        return success('审核成功',$id);
    }

}
